// 直接使用现有的数据库连接和模型
const { userModel, productModel, hospitalModel, doctorModel, bannerModel } = require('./db/models/cosmetic');

async function checkData() {
  try {
    console.log('🔍 开始检查数据库数据...\n');

    // 1. 检查医生数据
    console.log('📊 医生数据统计:');
    const totalDoctors = await doctorModel.countDocuments();
    console.log(`总医生数量: ${totalDoctors}`);

    // 检查成都的医生
    const chengduDoctors = await doctorModel.find({ city: '成都市' });
    console.log(`成都医生数量: ${chengduDoctors.length}`);
    
    if (chengduDoctors.length > 0) {
      console.log('成都医生详情:');
      chengduDoctors.forEach(doctor => {
        console.log(`- ${doctor.name} (${doctor.hospital}) - 专业: ${doctor.specialties.join(', ')}`);
      });
    }

    // 检查医生专业分布
    console.log('\n📈 医生专业分布:');
    const specialtyStats = await doctorModel.aggregate([
      { $unwind: '$specialties' },
      {
        $group: {
          _id: '$specialties',
          count: { $sum: 1 }
        }
      },
      { $sort: { count: -1 } },
      { $limit: 10 }
    ]);
    
    specialtyStats.forEach(item => {
      console.log(`- ${item._id}: ${item.count}人`);
    });

    // 2. 检查医院数据
    console.log('\n🏥 医院数据统计:');
    const totalHospitals = await hospitalModel.countDocuments();
    console.log(`总医院数量: ${totalHospitals}`);

    // 检查成都的医院
    const chengduHospitals = await hospitalModel.find({ city: '成都市' });
    console.log(`成都医院数量: ${chengduHospitals.length}`);
    
    if (chengduHospitals.length > 0) {
      console.log('成都医院详情:');
      chengduHospitals.forEach(hospital => {
        console.log(`- ${hospital.name} - 医生数量: ${hospital.doctors} - 评分: ${hospital.rating}`);
      });
    }

    // 3. 检查商品数据
    console.log('\n🛍️ 商品数据统计:');
    const totalProducts = await productModel.countDocuments();
    console.log(`总商品数量: ${totalProducts}`);

    // 检查成都相关的商品
    const chengduProducts = await productModel.find({ 
      hospital: { $regex: '成都', $options: 'i' } 
    });
    console.log(`成都相关商品数量: ${chengduProducts.length}`);
    
    if (chengduProducts.length > 0) {
      console.log('成都商品详情:');
      chengduProducts.forEach(product => {
        console.log(`- ${product.title} - 价格: ¥${product.price} - 医院: ${product.hospital}`);
      });
    }

    // 4. 检查医院分布聚合数据
    console.log('\n🗺️ 医院分布聚合数据:');
    const hospitalDistribution = await hospitalModel.aggregate([
      {
        $group: {
          _id: {
            province: '$province',
            city: '$city'
          },
          count: { $sum: 1 },
          hospitals: { 
            $push: { 
              name: '$name', 
              rating: '$rating',
              location: '$location',
              category: '$category',
              isHot: '$isHot',
              services: '$services',
              reviews: '$reviews',
              doctors: '$doctors'
            } 
          },
          avgRating: { $avg: '$rating' }
        }
      },
      { $sort: { count: -1 } }
    ]);

    console.log('城市医院分布:');
    hospitalDistribution.forEach(item => {
      const cityName = item._id.city || '未知城市';
      const totalDoctorsInCity = item.hospitals.reduce((sum, h) => sum + (h.doctors || 0), 0);
      console.log(`- ${cityName}: ${item.count}家医院, ${totalDoctorsInCity}个医生, 平均评分: ${item.avgRating.toFixed(1)}`);
      
      if (cityName === '成都市') {
        console.log(`  成都医院详情:`, item.hospitals);
      }
    });

    // 5. 检查核心指标
    console.log('\n📊 核心指标汇总:');
    console.log(`- 总用户数: ${await userModel.countDocuments()}`);
    console.log(`- 总商品数: ${totalProducts}`);
    console.log(`- 总医院数: ${totalHospitals}`);
    console.log(`- 总医生数: ${totalDoctors}`);

  } catch (error) {
    console.error('❌ 检查数据时出错:', error);
  }
}

checkData();